
package com.mvc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.entity.Loan;
import com.mvc.entity.Meminfo;
import com.mvc.service.LoanService;
import com.pub.MyJson;
import com.pub.Page;
import com.pub.StrUtils;

/**
 * 仓单质押贷款控制器
 * 
 * @author Hervey Hall
 *
 */
@Controller
@RequestMapping("Loan")
public class LoanController {
	protected final transient Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	LoanService loanService;

	/**
	 * 添加
	 * 
	 * @return
	 */
	@RequestMapping("Add.Action")
	public String add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
			HttpSession session) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		return "/Loan/Add";
	}

	/**
	 * 保存
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param session
	 */
	@RequestMapping("Save.Action")
	public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, HttpSession session) {
		String name = request.getParameter("name");
		String mobile = request.getParameter("mobile");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String address = request.getParameter("address");
		String loanAmount = request.getParameter("loan-amount");
		String usage = request.getParameter("loan-usage");
		String[] guaranteeMode = request.getParameterValues("guarantee-mode");
		String pawn = request.getParameter("pawn");
		String specification = request.getParameter("specification");
		String number = request.getParameter("number");
		String place = request.getParameter("production-place");
		String assets = request.getParameter("total-assets");
		String income = request.getParameter("annual-income");

		String msg = null;
		if (StrUtils.isNull(name)) {
			msg = "姓名/企业名称不能为空!";
		}
		if (StrUtils.isNull(mobile)) {
			msg = "手机号码不能为空!";
		}
		if (StrUtils.isNull(province)) {
			msg = "省份不能为空!";
		}
		if (StrUtils.isNull(city)) {
			msg = "市州不能为空!";
		}
		if (StrUtils.isNull(address)) {
			msg = "常驻地址/经营地址不能为空!";
		}
		if (StrUtils.isNull(loanAmount)) {
			msg = "申请额度不能为空!";
		}
		if (!loanAmount.matches("^(\\d+(\\.\\d*)?|\\.\\d+)$")) {
			msg = "申请额度格式不正确!";
		}
		if (StrUtils.isNull(usage)) {
			msg = "贷款用途不能为空!";
		}
		if (null == guaranteeMode || guaranteeMode.length == 0) {
			msg = "担保方式不能为空!";
		}
		StringBuilder guaranteeModeSb = new StringBuilder();
		for (String mode : guaranteeMode) {
			guaranteeModeSb.append(" ").append(mode);
		}
		if (guaranteeModeSb.length() > 0) {
			guaranteeModeSb.deleteCharAt(0);
		} else {
			msg = "担保方式不能为空!";
		}
		if (StrUtils.isNull(pawn)) {
			msg = "质押物不能为空!";
		}
		if (StrUtils.isNull(specification)) {
			msg = "规格不能为空!";
		}
		if (StrUtils.isNull(number)) {
			msg = "数量不能为空!";
		}
		if (!number.matches("^(\\d+(\\.\\d*)?|\\.\\d+)$")) {
			msg = "数量格式不正确!";
		}
		if (StrUtils.isNull(place)) {
			msg = "产地不能为空!";
		}
		if (!StrUtils.isNull(assets) && !assets.matches("^(\\d+(\\.\\d*)?|\\.\\d+)$")) {
			msg = "总资产格式不正确!";
		}
		if (!StrUtils.isNull(income) && !income.matches("^(\\d+(\\.\\d*)?|\\.\\d+)$")) {
			msg = "年收入格式不正确!";
		}

		MyJson mj = new MyJson();
		if (null == msg) {
			Loan loan = new Loan();
			loan.setName(name).setMobile(mobile).setProvince(province).setCity(city).setAddress(address)
					.setLoanAmount(Double.parseDouble(loanAmount)).setLoanUsage(usage)
					.setGuaranteeMode(guaranteeModeSb.toString()).setPawn(pawn).setSpecification(specification)
					.setNumber(Double.parseDouble(number)).setPlace(place)
					.setAsserts(StrUtils.isNull(assets) ? null : Double.parseDouble(assets))
					.setIncome(StrUtils.isNull(income) ? null : Double.parseDouble(income));
			loanService.save(loan);
			mj.setStatusCode(200);
			mj.setMessage("保存成功!");
		} else {
			mj.setStatusCode(300);
		}
		mj.setForwardUrl("back");
		mj.setCallbackType("forward");
		String str = mj.getJson();
		try {
			response.getWriter().println(str);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 贷款管理首页
	 * 
	 * @return
	 */
	@RequestMapping("Index.do")
	public String index(HttpSession session, HttpServletRequest request, ModelMap modelMap, String pageNo,
			String Ajax) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		Page page = new Page();
		page.setPageNox(pageNo);
		page.setPageSize(15);
		List<Object> loanList = loanService.getLoanList(page);
		page.setPageCount(Long.valueOf(loanService.getLoanCount(null, page)).intValue());
		modelMap.put("list", loanList);
		modelMap.put("page", page);
		if (Ajax != null && Ajax.equals("Ajax")) {
			return "/Goodsorder/List";
		}
		return "/Loan/Index";
	}

	/**
	 * 查看详情
	 * 
	 * @return
	 */
	@RequestMapping("Detail.do")
	public String detail(HttpSession session, HttpServletRequest request, ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);

		String idStr = request.getParameter("ID");
		Long id = Long.parseLong(idStr);
		Loan loan = loanService.getLoan(id);
		modelMap.put("loan", loan);
		return "/Loan/Detail";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping("Del.do")
	public void delete(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {
		Meminfo smeminfo = (Meminfo) session.getAttribute("SessionMem");
		modelMap.put("smeminfo", smeminfo);
		String idStr = request.getParameter("ID");
		Long id = Long.parseLong(idStr);
		loanService.deleteLoan(id);

		String msg = "数据删除成功";
		MyJson mj = new MyJson();
		mj.setStatusCode(200);
		mj.setMessage(msg);
		mj.setForwardUrl("/Loan/Index.do");
		mj.setCallbackType("forward");
		try {
			response.getWriter().print(mj.getJson());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
